"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

from PyQt4 import QtCore
from PyQt4 import QtGui
from msnp.gui import GUIHelper, UIText, Resources, MyStyle, MySignal
from msnp import gui
from msnp.service import network
from msnp.persistence import XMLController, PersistenceController
from msnp.service.network import NetworkController
from msnp.model import PTPModel, MediaModel
from msnp.logger import Logger
from msnp.service.network.model import Model
from msnp.model.SettingsModel import GroupSettings
from msnp.model.MediaModel import NetSynchChanges

class GroupWidget(QtGui.QDialog):
    """This class represents a group widget, which either enables the user to search for groups or displays a certain group.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __mainLayout: A QVBoxLayout object containing one of the items __searchWidget, __groupDetailWidget or __groupSplashWidget.
        __settings: A Settings object.
        __searchWidget: A SearchWidget item.
        __groupDetailWidget: A GroupDetailsWidget item.
        __groupSplashWidget: A GroupDetailsSplashWidget item.
    """
    def __init__(self, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__settings = XMLController.readAllSettings()
        self.__searchWidget = None
        self.__groupDetailWidget = None
        self.__groupSplashWidget = None
        self.__mainLayout = QtGui.QVBoxLayout()
        self.setLayout(self.__mainLayout)
#        self.setModal(True)
        GUIHelper.centerWidget(self)
        
    def buildSearchWidget(self, isBack=False):
        """Sets the item __searchWidget by creating a new SearchWidget item."""
        self.setWindowTitle(UIText.TITLE_GROUP_SEARCH)
        self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_GROUP_ICON))
        self.__cleanLayout()
        if self.__searchWidget == None or isBack == False:
            self.__searchWidget = SearchWidget(self)
        self.__mainLayout.addWidget(self.__searchWidget)
        self.setFixedSize(630, 550)
        self.repaint()
        GUIHelper.centerWidget(self)
        
    def buildGroupSplashWidget(self):
        """Sets the item __groupSplashWidget by creating a new GroupDetailsSplashWidget item."""
        self.setWindowTitle(UIText.TITLE_PLEASE_WAIT)
        self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_GROUP_ICON))
        self.__cleanLayout()
        self.__groupSplashWidget = GroupDetailsSplashWidget()
        self.__mainLayout.addWidget(self.__groupSplashWidget)
        self.setFixedSize(630, 550)
        self.repaint()
        GUIHelper.centerWidget(self)
        
    def buildGroupDetailWidget(self, groupDetailType, group):
        """Sets the item __groupDetailsWidget by creating a new GroupDetailsWidget item."""
        self.setWindowTitle(UIText.TITLE_GROUP_DETAILS)
        if groupDetailType == GUIHelper.GroupDetailType.NEW_GROUP:
            self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_NEW_GROUP))
        else: 
            self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_GROUP_ICON))
        self.__cleanLayout()
        #if group was searched the searchwidget is != None
        fromSearch = self.__searchWidget != None
        self.__groupDetailWidget = GroupDetailsWidget(groupDetailType, group, fromSearch, self.__settings, self) 
        self.__mainLayout.addWidget(self.__groupDetailWidget)
        self.setFixedSize(630, 550)
        self.repaint()
        GUIHelper.centerWidget(self)
        
    # remove widgets from layout when building a new one
    def __cleanLayout(self):
        """This method removes all items from the __mainLayout."""
        import sip
        if self.layout() != None:
            oldLayout = self.layout()
            for i in reversed(range(oldLayout.count())):
                oldLayout.itemAt(i).widget().setParent(None)
            sip.delete(oldLayout)
        self.__mainLayout = QtGui.QVBoxLayout()
        self.setLayout(self.__mainLayout)
        
    def getSearchWidget(self):
        """Returns the item __searchWidget."""
        return self.__searchWidget

    def closeEvent(self, event):
        """This method is called when the widget is closing."""
        gui.GUIController.closeGroupWidget()
        event.accept()
        
class SearchWidget(QtGui.QWidget):
    """This class represents a group search widget which enables the user to search for groups.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __mainLayout: A QVBoxLayout object containing the items __titleWidget, __editSearchWidget, __searchResultWidget and __buttonWidget.
        __titleWidget: A QWidget object containing the items __checkSearchAll and __lblLoading.
        __checkSearchAll: A QCheckBox object connected to the method __checkSearchAllChanged().
        __lblLoading: A QLabel object containing the item __movie.
        __movie: A QMovie object.
        __editSearchWidget: A QWidget object containing the items __lblGroupname, __lblOwnername, __lblGenre, __txtGroupname, __txtOwnername and __cmbGenre.
        __lblGroupname: A QLabel object.
        __lblOwnername: A QLabel object.
        __lblGenre: A QLabel object.
        __txtGroupname: A QLineEdit object.
        __txtOwnername: A QLineEdit object.
        __cmbGenre: A QComboBox object.
        __searchResultWidget: A SearchResultWidget object.
        __buttonWidget: A QWidget object containing the items __butSearch and __butDiscard.
        __butSearch: A QPushButton object connected to the method __search().
        __butDiscard: A QPushButton object connected to the method __discard().
    """
    def __init__(self, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__parent = parent
        self.__mainLayout = QtGui.QVBoxLayout()
        #build
        self.__build()
        self.setLayout(self.__mainLayout)
        
    def __build(self):
        """Initializes all items of the widget and adds them to the item __mainLayout."""
        self.__initLabels()
        self.__initLineEdits()
        self.__initCheckBox()
        self.__initComboBox()
        self.__initTitleWidget()
        self.__initButtonWidget()
        self.__initSearchEditWidget()
        self.__initSearchResultList()
        self.__mainLayout.addWidget(self.__titleWidget)
        self.__mainLayout.addWidget(self.__editSearchWidget)
        self.__mainLayout.addWidget(self.__searchResultWidget)
        self.__mainLayout.addWidget(self.__buttonWidget)
        
    def __initTitleWidget(self):
        """Initializes the item __titleWidget and adds its contained items."""
        hLayout = QtGui.QHBoxLayout()
        hLayout.addWidget(self.__checkSearchAll)
        hLayout.addWidget(self.__lblLoading)
        self.__titleWidget = QtGui.QWidget()
        self.__titleWidget.setFixedHeight(50)
        self.__titleWidget.setLayout(hLayout)
        
    def __initSearchEditWidget(self):
        """Initializes the item __editSearchWidget and adds its contained items."""
        grid = QtGui.QGridLayout()
        grid.addWidget(self.__lblGroupame, 1, 0)
        grid.addWidget(self.__lblOwnername, 2, 0)
        grid.addWidget(self.__lblGenre, 3, 0)
        grid.addWidget(self.__txtGroupname, 1, 1)
        grid.addWidget(self.__txtOwnername, 2, 1)
        grid.addWidget(self.__cmbGenre, 3, 1)
        self.__editSearchWidget = QtGui.QWidget()
        self.__editSearchWidget.setLayout(grid)
    
    def __initLabels(self):
        """Initializes the QLabel items __lblGroupname, __lblOwnername, __lblGenre and __lblLoading and the QMovie object __movie."""
        self.__lblGroupame = QtGui.QLabel(UIText.LABEL_GROUP_NAME + ":")
        self.__lblGroupame.setFont(MyStyle.getFont13Bold())
        self.__lblOwnername = QtGui.QLabel(UIText.LABEL_OWNER_NAME + ":")
        self.__lblOwnername.setFont(MyStyle.getFont13Bold())
        self.__lblGenre = QtGui.QLabel(UIText.LABEL_GENRE + ":")
        self.__lblGenre.setFont(MyStyle.getFont13Bold())
        
        #movie container
        self.__lblLoading = QtGui.QLabel()
#        self.__lblSplash.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.__lblLoading.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        self.__lblLoading.setFont(MyStyle.getFont8())
        palette = self.__lblLoading.palette()
        palette.setColor(self.__lblLoading.foregroundRole(), QtCore.Qt.darkGreen)
        self.__lblLoading.setPalette(palette)
        #create movie
        self.__movie = QtGui.QMovie(Resources.IMAGE_LOADING, QtCore.QByteArray(), self)
        self.__movie.setCacheMode(QtGui.QMovie.CacheAll)
        self.__movie.setSpeed(80)
        
    def __initCheckBox(self):
        """Initializes the QCheckBox object __checkSearchAll and connects it to the method __checkSearchAllChanged()."""
        self.__checkSearchAll = QtGui.QCheckBox(UIText.LABEL_SEARCH_ALL_GROUPS)
        self.__checkSearchAll.setChecked(True)
        self.connect(self.__checkSearchAll, QtCore.SIGNAL('stateChanged (int)'), self.__checkSearchAllChanged)
        
    def __initLineEdits(self):
        """Initializes the QLineEdit object __txtGroupname and __txtOwnername."""
        self.__txtGroupname = QtGui.QLineEdit()
        self.__txtGroupname.setEnabled(False)
        self.__txtOwnername = QtGui.QLineEdit()
        self.__txtOwnername.setEnabled(False)
        
    def __initComboBox(self):
        """Initializes the QComboBox object __cmbGenre."""
        self.__cmbGenre = QtGui.QComboBox()
        genres = PersistenceController.selectPredefinedGenres()
        self.__cmbGenre.insertItem(0, "-- all genres --")
        for genre in genres:
            self.__cmbGenre.insertItem(0, genre.getName())
        self.__cmbGenre.setEnabled(False)
        
    def __initButtonWidget(self):
        """Initializes the item __buttonWidget and adds its contained items."""
        buttonLayout = QtGui.QHBoxLayout()
        self.__initButtons()
        buttonLayout.addWidget(self.__butDiscard)
        buttonLayout.addWidget(self.__butSearch)
        self.__buttonWidget = QtGui.QWidget()
        self.__buttonWidget.setLayout(buttonLayout)
        
    def __initButtons(self):
        """Initializes the QPushButton objects __butSearch and __butDiscard."""
        #save
        self.__butSearch = QtGui.QPushButton(UIText.BUT_SEARCH, self)
        self.__butSearch.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butSearch.setIcon(QtGui.QIcon(Resources.IMAGE_SEARCH_ICON))
        #discard
        self.__butDiscard = QtGui.QPushButton(UIText.BUT_DISCARD, self)
        self.__butDiscard.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butDiscard.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_DISCARD))
        #register handler
        self.connect(self.__butSearch, QtCore.SIGNAL('clicked()'), self.__search)
        self.connect(self.__butDiscard, QtCore.SIGNAL('clicked()'), self.__discard)
        
    def __initSearchResultList(self):
        """Initializes the item __searchResultWidget by creating a new SearchResultWidget object."""
        self.__searchResultWidget = SearchResultWidget(self)
        
    def addSearchResult(self, group):
        """Calls the method addSearchResult() of the __searchResultWidget item with the given value."""
        self.__searchResultWidget.addSearchResult(group)
        
    def stopLoadingMovie(self):
        """Resets the QLabel object __lblLoading and calls the method searchFinished() of the item __searchResultWidget."""
        Logger.guiInfo('movie stopped')
        self.__movie.stop()
        self.__lblLoading.setMovie(None)
        self.__lblLoading.setText(UIText.MSG_SEARCH_FINISHED)
        self.__searchResultWidget.searchFinished()
        
    def __checkSearchAllChanged(self):
        """If the item __checkSearchAll is enabled the items __txtGroupname, __txtOwnername and __cmbGenre are disabled and vice versa."""
        self.__txtGroupname.setEnabled(not self.__checkSearchAll.isChecked())
        self.__txtOwnername.setEnabled(not self.__checkSearchAll.isChecked())
        self.__cmbGenre.setEnabled(not self.__checkSearchAll.isChecked())
    
    def __search(self):
        """This method is connected to the QPushButton object __butSearch. It initializes a search for groups in the network."""
        Logger.guiInfo('search')
        self.__lblLoading.setMovie(self.__movie)
        self.__movie.start()
        self.__searchResultWidget.emptyList()
        groupname = unicode(str(self.__txtGroupname.text()))
        ownername = unicode(str(self.__txtOwnername.text()))
        displayname = None
        genreName = unicode(str(self.__cmbGenre.currentText()))
        genre = None
        if genreName != "-- all genres --":
            genre = PersistenceController.selectPredefinedGenreByName(genreName)
        if self.__checkSearchAll.isChecked():
            NetworkController.getGroups(None, None, None, None, Model.CallbackReactType.DISPLAYGROUP)
        else:
            NetworkController.getGroups(ownername, groupname, displayname, genre, Model.CallbackReactType.DISPLAYGROUP)
        
    def __discard(self):
        """This method is connected to the QPushButton object __butSearch. It closes the parent widget."""
        self.__parent.close()
        
    def getParent(self):
        """Returns the item __parent."""
        return self.__parent
        
#the list -> element of the searchwidget
class SearchResultWidget(QtGui.QListWidget):
    """This class represents a group search result widget, which displays the retrieved groups.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __items: A list of QListWidgetItem objects corresponding to the items of the list __groups.
        __groups: A list of Group objects to display.
    """
    def __init__(self, parent=None):
        """The init-method."""
        QtGui.QListWidget.__init__(self, parent)
        self.__parent = parent
        self.connect(self, QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem*)"), self.__doubleClick)
        self.__items = []
        self.__groups = []
        
    def addSearchResult(self, group):
        """Adds the given group to the list __groups, creates a corresponding QListWidgetItem and adds it to the list __items."""
        #don't want to display multiple equal results
        isNewGroup = True
        if self.__groups != None:
            if len(self.__groups) == 0:
                # remove the 'no group found' item if present
                if len(self.__items) > 0:
                    print "removed 'no groups received' item"
                    self.emptyList()
            else:
                for g in self.__groups:
                    if g.getGroupname() == group.getGroupname() and g.getOwnername() == group.getOwnername():
                        isNewGroup = False
        else:
            self.__groups = []
        if isNewGroup:
            item = QtGui.QListWidgetItem()
            item.setIcon(QtGui.QIcon(Resources.IMAGE_GROUP_ICON))
            item.setText(group.getGroupname() + " (" + group.getOwnername() + ")")
            item.setTextAlignment(QtCore.Qt.AlignLeft)
            item.setFont(MyStyle.getFont13())
            self.__items.append(item)
            self.__groups.append(group)
            self.addItem(item)

    def emptyList(self):
        """Removes all items from the list __items and __groups."""
        for item in self.__items:
            self.takeItem(self.row(item))
        self.__groups = []
        self.__items = []
    
    def searchFinished(self):
        """If the list __groups is empty it appends a no-groups-found item to the list __items."""
        if self.__groups == None or len(self.__groups) == 0:
            item = QtGui.QListWidgetItem()
            item.setText(UIText.LABEL_NO_GROUPS_FOUND)
            item.setTextAlignment(QtCore.Qt.AlignCenter)
            item.setFont(MyStyle.getFont13())
            self.__items.append(item)
            self.addItem(item)
        
    def __doubleClick(self):
        """This method is connected to an object of this class.
        The current selected item is retrieved and a emitSearchGroupDetailsFinishedSignal is emitted with that group."""
        selectedRow = self.currentRow()
        if self.__groups != None and len(self.__groups) > 0:
            selectedGroup = self.__groups[selectedRow]
            #splash no more needed because we have already the group details
            MySignal.emitSearchGroupDetailsFinishedSignal(selectedGroup)


class GroupDetailsSplashWidget(QtGui.QWidget):
    """This class represents a group details splash widget displayed while waiting for the retrieval of the group details.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __mainLayout: A QVBoxLayout object containing the items __lblSplash and __lblLoading.
        __lblSplash: A QLabel object.
        __lblLoading: A QLabel object containing the item __movie.
        __movie: A QMovie object.
    """
    def __init__(self, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__mainLayout = QtGui.QVBoxLayout()
        self.__build()
        self.setLayout(self.__mainLayout)
        
    def __build(self): 
        """Initializes the item __mainLayout and adds its contained items."""
        self.__initSplashLabels()
        self.__mainLayout.addWidget(self.__lblSplash)
        self.__mainLayout.addWidget(self.__lblLoading)
        
    def __initSplashLabels(self):
        """Initializes the QLabel objects __lblSplash, __lblLoading and the QMovie object __movie."""
        #movie container
        self.__lblSplash = QtGui.QLabel()
#        self.__lblSplash.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.__lblSplash.setAlignment(QtCore.Qt.AlignCenter)
        #create movie
        self.__movie = QtGui.QMovie(Resources.IMAGE_LOADING, QtCore.QByteArray(), self)
        self.__movie.setCacheMode(QtGui.QMovie.CacheAll)
        self.__movie.setSpeed(100)
        self.__lblSplash.setMovie(self.__movie)
        self.__movie.start()
        self.__lblLoading = QtGui.QLabel(UIText.LABEL_LOADING_GROUP_DETAILS)
        self.__lblLoading.setAlignment(QtCore.Qt.AlignHCenter)
        
    
class GroupDetailsWidget(QtGui.QWidget):
    """This class represents a group details widget which displays a certain group.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __mainLayout: A QVBoxLayout object containing the items __titleWidget, __tabWidget and __buttonWidget.
        __settings: A Settings object.
        __groupDetailType: A GroupDetailType object depending on it different items are added to this widget, either NEW_GROUP, OWNER, MEMBER or NOT_MEMBER.
        __group: A Group object, depending on it the widget is created.
        __fromSearch: A boolean value, depending on it the widget is created.
        __titleWidget: A QWidget object containing one or more of the items __lblDisabled, __lblEnterName, __lblOwner, __lblGroupName and __txtGroupName.
        __lblDisabled: A QLabel object.
        __lblEnterName: A QLabel object.
        __lblOwner: A QLabel object.
        __lblGroupName: A QLabel object.
        __txtGroupName: A QLineEdit object.
        __tabWidget: A QWidget object containing the items __tabGeneral and __tabMembers.
        __tabGeneral: A GroupDetailTabGeneral object.
        __tabMembers: A GroupDetailTabMembers object.
        __buttonWidget: A QWidget object containing one or more of the items __butDiscard, __butCreate, __butBack, __butDisable, __butLeave and __butJoin.
        __butDiscard: A QPushButton object connected to the method __discard().
        __butCreate: A QPushButton object connected to the method __create().
        __butBack: A QPushButton object connected to the method __back().
        __butDisable: A QPushButton object connected to the method __disable().
        __butLeave: A QPushButton object connected to the method __leave().
        __butJoin: A QPushButton object connected to the method __join().
    """
    def __init__(self, groupDetailType, group, fromSearch, settings, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        Logger.guiInfo('group details opened with type ' + groupDetailType)
        self.__settings = settings
        self.__parent = parent
        self.__groupDetailType = groupDetailType
        self.__group = group
        self.__fromSearch = fromSearch
        self.__build()
        
    def __build(self):
        """Initializes the item __mainLayout and adds its contained items."""
        self.__cleanLayout()
        self.__initTitleWidget()
        self.__initTabs()
        self.__initButtonWidget()
        self.__mainLayout.addWidget(self.__titleWidget)
        self.__mainLayout.addWidget(self.__tabWidget)
        self.__mainLayout.addWidget(self.__buttonWidget)
        
    def __cleanLayout(self):
        """Removes all items from the item __mainLayout."""
        import sip
        if self.layout() != None:
            oldLayout = self.layout()
            for i in reversed(range(oldLayout.count())):
                oldLayout.itemAt(i).widget().setParent(None)
            sip.delete(oldLayout)
        self.__mainLayout = QtGui.QVBoxLayout()
        self.setLayout(self.__mainLayout)
    
    def __initButtonWidget(self):
        """Initializes the item __buttonWidget and adds its contained items."""
        buttonLayout = QtGui.QHBoxLayout()
        #new group
        if self.__groupDetailType == GUIHelper.GroupDetailType.NEW_GROUP:
            self.__initButDiscard()
            self.__initButCreate()
            buttonLayout.addWidget(self.__butDiscard)
            buttonLayout.addWidget(self.__butCreate)
        else:
            if self.__fromSearch:
                self.__initButBack()
                buttonLayout.addWidget(self.__butBack)
            else:
                self.__initButDiscard()
                buttonLayout.addWidget(self.__butDiscard)
            #owner
            if self.__groupDetailType == GUIHelper.GroupDetailType.OWNER:
                if self.__group != None:
                    if self.__group.isActive():
                        self.__initButDisable()
                    else:
                        self.__initButEnable()
                self.__initButSave()
                buttonLayout.addWidget(self.__butDisable)
                buttonLayout.addWidget(self.__butSave)
            #member
            elif self.__groupDetailType == GUIHelper.GroupDetailType.MEMBER:
                self.__initButLeave()
                buttonLayout.addWidget(self.__butLeave)
            elif self.__groupDetailType == GUIHelper.GroupDetailType.NOT_MEMBER:
                self.__initButJoin()
                buttonLayout.addWidget(self.__butJoin)
            else:
                Logger.guiInfo('unknown groupdetailtype in initButtonWidget of groupdetailswidget: ' + self.__groupDetailType)
        self.__buttonWidget = QtGui.QWidget()
        self.__buttonWidget.setLayout(buttonLayout)
        
    def __initTitleWidget(self):
        """Initializes the item __titleWidget and adds its contained items."""
        vLayout = QtGui.QVBoxLayout()
        if self.__group != None and self.__group.isActive() == False:
            self.__initLblDisabled()
            vLayout.addWidget(self.__lblDisabled)
        self.__initLblOwner()
        if self.__groupDetailType == GUIHelper.GroupDetailType.NEW_GROUP:
            self.__initLblEnterName()
            self.__initTxtGroupName()
            vLayout.addWidget(self.__lblEnterName)
            vLayout.addWidget(self.__txtGroupName)
        else:
            self.__initLblGroupName()
            vLayout.addWidget(self.__lblGroupName)
        vLayout.addWidget(self.__lblOwner)
        self.__titleWidget = QtGui.QWidget()
        self.__titleWidget.setLayout(vLayout)
        
    def __initTxtGroupName(self):
        """Initializes the item __txtGroupName."""
        #textfield for groupname
        self.__txtGroupName = QtGui.QLineEdit()
        self.__txtGroupName.setFont(MyStyle.getFont18Bold())
        self.__txtGroupName.setAlignment(QtCore.Qt.AlignCenter)
        
    def __initLblEnterName(self):
        """Initializes the item __lblEnterName."""
        self.__lblEnterName = QtGui.QLabel(UIText.LABEL_ENTER_GROUP_NAME + ":")
        self.__lblEnterName.setAlignment(QtCore.Qt.AlignLeft)
        self.__lblEnterName.setFont(MyStyle.getFont8())
        
    def __initLblGroupName(self):
        """Initializes the item __lblGropuName."""
        #label for groupname
        self.__lblGroupName = QtGui.QLabel()
        if self.__group != None:
            self.__lblGroupName.setText(self.__group.getGroupname())
            if self.__group.isActive() == False:
                self.__lblGroupName.setDisabled(True)
        self.__lblGroupName.setFont(MyStyle.getFont18Bold())
        self.__lblGroupName.setAlignment(QtCore.Qt.AlignCenter)
        
    def __initLblOwner(self):
        """Initializes the QLabel object __lblOwner."""
        if self.__groupDetailType == GUIHelper.GroupDetailType.OWNER or self.__groupDetailType == GUIHelper.GroupDetailType.NEW_GROUP:
            self.__lblOwner = QtGui.QLabel(UIText.MSG_YOU_ARE_THE_OWNER)
        else:
            self.__lblOwner = QtGui.QLabel(UIText.LABEL_OWNER + ": " + self.__group.getOwnername())
        if self.__group != None and self.__group.isActive() == False:
            self.__lblOwner.setEnabled(False)
        self.__lblOwner.setAlignment(QtCore.Qt.AlignRight)
        self.__lblOwner.setFont(MyStyle.getFont8())
    
    def __initLblDisabled(self):
        """Initializes the QLabel object __lblDisabled."""
        self.__lblDisabled = QtGui.QLabel(UIText.LABEL_GROUP_DISABLED)
        self.__lblDisabled.setAlignment(QtCore.Qt.AlignLeft)
        self.__lblDisabled.setFont(MyStyle.getFont8())
        
    def __initTabs(self):
        """Initializes the item __tabWidget and adds its contained items."""
        self.__tabWidget = QtGui.QTabWidget(self)
        self.__tabGeneral = GroupDetailTabGeneral(self.__group, self.__groupDetailType)
        self.__tabMembers = GroupDetailTabMembers(self.__group, self.__groupDetailType, self.__settings)
        self.__tabWidget.addTab(self.__tabGeneral, UIText.LABEL_GENERAL)
        self.__tabWidget.addTab(self.__tabMembers, UIText.LABEL_MEMBERS)
        
    def __initButSave(self):
        """Initializes the QPushButton object __butSave and connects it to the method __save()."""
        self.__butSave = QtGui.QPushButton(UIText.BUT_SAVE, self)
        self.__butSave.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butSave.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_SAVE))
        if self.__group != None and self.__group.isActive() == False:
            self.__butSave.setDisabled(True)
        self.connect(self.__butSave, QtCore.SIGNAL('clicked()'), self.__save)
        
    def __initButCreate(self):
        """Initializes the QPushButton object __butCreate and connects it to the method __create()."""
        self.__butCreate = QtGui.QPushButton(UIText.BUT_CREATE_GROUP, self)
        self.__butCreate.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butCreate.setIcon(QtGui.QIcon(Resources.IMAGE_NEW_GROUP))
        self.connect(self.__butCreate, QtCore.SIGNAL('clicked()'), self.__create)
        
    def __initButDiscard(self):
        """Initializes the QPushButton object __butDiscard and connects it to the method __discard()."""
        self.__butDiscard = QtGui.QPushButton(UIText.BUT_DISCARD, self)
        self.__butDiscard.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butDiscard.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_DISCARD))
        self.connect(self.__butDiscard, QtCore.SIGNAL('clicked()'), self.__discard)
        
    def __initButBack(self):
        """Initializes the QPushButton object __butBack and connects it to the method __back()."""
        self.__butBack = QtGui.QPushButton(UIText.BUT_BACK, self)
        self.__butBack.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butBack.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_BACK))
        self.connect(self.__butBack, QtCore.SIGNAL('clicked()'), self.__back)
        
    def __initButJoin(self):
        """Initializes the QPushButton object __butJoin and connects it to the method __join()."""
        self.__butJoin = QtGui.QPushButton(UIText.BUT_JOIN_GROUP, self)
        self.__butJoin.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butJoin.setIcon(QtGui.QIcon(Resources.IMAGE_CONFIRM_MEMBERS))
        if self.__group != None and self.__group.isActive() == False:
            self.__butJoin.setDisabled(True)
        self.connect(self.__butJoin, QtCore.SIGNAL('clicked()'), self.__join)
        
    def __initButLeave(self):
        """Initializes the QPushButton object __butLeave and connects it to the method __leave()."""
        self.__butLeave = QtGui.QPushButton(UIText.BUT_LEAVE_GROUP, self)
        self.__butLeave.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butLeave.setIcon(QtGui.QIcon(Resources.IMAGE_LOGOUT))
        if self.__group != None and self.__group.isActive() == False:
            self.__butLeave.setDisabled(True)
        self.connect(self.__butLeave, QtCore.SIGNAL('clicked()'), self.__leave)
        
    def __initButDisable(self):
        """Initializes the QPushButton object __butDisable and connects it to the method __disable()."""
        self.__butDisable = QtGui.QPushButton(UIText.BUT_DISABLE_GROUP, self)
        self.__butDisable.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butDisable.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_DISABLE))
        self.connect(self.__butDisable, QtCore.SIGNAL('clicked()'), self.__disable)
        
    def __initButEnable(self):
        """Initializes the QPushButton object __butDiscard and connects it to the method __enable()."""
        self.__butDisable = QtGui.QPushButton(UIText.BUT_ENABLE_GROUP, self)
        self.__butDisable.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butDisable.setIcon(QtGui.QIcon(Resources.IMAGE_NEW_GROUP))
        self.connect(self.__butDisable, QtCore.SIGNAL('clicked()'), self.__enable)        
        
    def __save(self):
        """This method is connected to the QPushButton object __butSave."""
        groupname = unicode(str(self.__lblGroupName.text()))
        description = self.__tabGeneral.getDescription()
        genres = self.__tabGeneral.getGenres()
        NetworkController.changeGroupDetails(groupname, groupname, description, genres, True)
        
    def __create(self):
        """This method is connected to the QPushButton object __butCreate."""
        groupname = unicode(str(self.__txtGroupName.text()))
        if self.__validate(groupname):
            genres = self.__tabGeneral.getGenres()
            reply = QtGui.QMessageBox.Yes
            if genres == None or len(genres) == 0:
                reply = QtGui.QMessageBox.question(self, UIText.MSG_ATTENTION, UIText.MSG_CREATE_GROUP_WITHOUT_GENRES  , QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
            if reply == QtGui.QMessageBox.Yes:
                mediaDBSettings = XMLController.readAllSettings().getCurrentUserSettings().getMediaDatabaseSettings()
                if mediaDBSettings.isComplete():
                    description = self.__tabGeneral.getDescription()
                    (newGroup, retValue) = NetworkController.establishGroup(groupname, groupname, description, genres)
                    if newGroup != None:
                        MySignal.emitSearchGroupDetailsFinishedSignal(newGroup)
                        MySignal.emitRefreshGroupListSignal(newGroup)
                        #write group into settings
                        settings = XMLController.readAllSettings()
                        groupSettings = settings.getCurrentUserSettings().getGroupSettingsList()
                        groupSettings.append(GroupSettings(groupname, settings.getCurrentUserSettings().getUsername(), True, True))
                        XMLController.storeSettings(settings)
                        # write netsynch with type INITIAL
                        netSynchChange = NetSynchChanges(None, groupname, settings.getCurrentUserSettings().getUsername(), None, None, True, MediaModel.NetSynchChangeSource.INITIAL)
                        PersistenceController.insertNetSynchChange(netSynchChange)
                    else:
                        QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_GROUP_CREATION_FAILED + PTPModel.ReturnType.returnTypeToString(retValue), UIText.BUT_OK)
                else:
                    QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_GROUP_CREATE_FAILED_MEDIA_DB, UIText.BUT_OK)
                
    
    def __validate(self, groupname):
        """This method checks whether the given string is valid for a groupname."""
        if groupname != None and len(groupname) > 0:
            if len(groupname) < 200:
                withoutWhitespace = groupname.expandtabs().replace(" ", "")
                if len(withoutWhitespace) > 0:
                    return True
                else:
                    QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_WHITESPACE_GROUPNAME, UIText.BUT_OK)
            else:
                QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_TOO_LONG_GROUPNAME, UIText.BUT_OK)
        else:
            QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_ENTER_GROUPNAME, UIText.BUT_OK)
        return False
    
    def __discard(self):
        """This method is connected to the QPushButton object __butDiscard."""
        self.__parent.close()
        
    def __back(self):
        """This method is connected to the QPushButton object __butBack."""
        gui.GUIController.getGroupWidget().buildSearchWidget(True)
        
    def __join(self):
        """This method is connected to the QPushButton object __butJoin."""
        mediaDBSettings = XMLController.readAllSettings().getCurrentUserSettings().getMediaDatabaseSettings()
        if mediaDBSettings.isComplete():
            groupname = self.__group.getGroupname()
            ownername = self.__group.getOwnername()
            NetworkController.addAsMember(groupname, ownername)
            MySignal.emitRefreshGroupListSignal(self.__group)
            #add member with status not_confirmed
#            self.__group.addMember(self.__settings.getCurrentUserSettings().getUsername(), PTPModel.MemberStatus.NOT_CONFIRMED)
            self.__groupDetailType = GUIHelper.GroupDetailType.MEMBER
            self.__build()
            QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_GROUP_JOIN_MESSAGE_SENT, UIText.BUT_OK)
        else:
            QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_GROUP_JOIN_FAILED_MEDIA_DB, UIText.BUT_OK)
        
    def __leave(self):
        """This method is connected to the QPushButton object __butLeave."""
        reply = QtGui.QMessageBox.question(self, UIText.MSG_ATTENTION, UIText.MSG_REALLY_LEAVE_MESSAGE  , QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            membername = self.__settings.getCurrentUserSettings().getUsername()
            groupname = self.__group.getGroupname()
            ownername = self.__group.getOwnername()
            NetworkController.changeMemberStatus(membername, groupname, ownername, PTPModel.MemberStatus.DELETED)
            
            #change status to deleted
            self.__group.setMemberStatus(self.__settings.getCurrentUserSettings().getUsername(), PTPModel.MemberStatus.DELETED)
            self.__groupDetailType = GUIHelper.GroupDetailType.NOT_MEMBER
            self.__build()
            
            #remove group from grouplist
            MySignal.emitRefreshGroupListSignal(self.__group, True)

    def __disable(self):
        """This method is connected to the QPushButton object __butDisable."""
        reply = QtGui.QMessageBox.question(self, UIText.MSG_ATTENTION, UIText.MSG_REALLY_DISABLE_MESSAGE, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            self.__group.setStatus(False)
            groupname = self.__group.getGroupname()
            description = self.__group.getDescription()
            genres = self.__group.getGenres()
            isActive = False
            network.NetworkController.changeGroupDetails(groupname, None, description, genres, isActive)
            self.__build()

    def __enable(self):
        """This method is connected to the QPushButton object __butDisable."""
        reply = QtGui.QMessageBox.question(self, UIText.MSG_ATTENTION, UIText.MSG_REALLY_ENABLE_MESSAGE, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            self.__group.setStatus(True)
            groupname = self.__group.getGroupname()
            description = self.__group.getDescription()
            genres = self.__group.getGenres()
            isActive = True
            network.NetworkController.changeGroupDetails(groupname, None, description, genres, isActive)
            self.__build()
        

class GroupDetailTabGeneral(QtGui.QWidget):
    """This class represents the group details tab of the group details widget which displays all information about the group except its members.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __mainLayout: A QVBoxLayout object containing the items __descriptionWidget and __genreFullWidget.
        __group: The group the widget displays.
        __groupDetailType: Indicates how the group details should be displayed, one of OWNER, MEMBER, NOT_MEMBER or NEW_GROUP.
        __genres: A list of Genre objects containing all predefined genres.
        __descriptionWidget: A QWidget object containing the items __lblDescription and __txtDescription.
        __lblDescription: A QLabel object.
        __txtDescription: A QTextEdit object.
        __genreFullWidget: A QWidget object containing the items __genreArea, __lblGenre and eventually __buttonWidget.
        __genreArea: A QScrollArea object containing the objects of the list __checkGenres.
        __lblGenre: A QLabel object.
        __buttonWidget: A QWidget object containing the items __butSelectAll and __butDeselectAll.
        __butSelectAll: A QPushButton object connected to the method __selectAll().
        __butDeselectAll: A QPushButton object connected to the method __deselectAll().
        __checkGenres: A list of GenreCheckBox objects.
    """
    def __init__(self, group, groupDetailType, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__group = group
        self.__groupDetailType = groupDetailType
        self.__genres = PersistenceController.selectPredefinedGenres()
        self.__mainLayout = QtGui.QVBoxLayout()
        self.__build()
        self.setLayout(self.__mainLayout)
        
    def __build(self):
        """Initializes the item __mainLayout and adds its contained items."""
        self.__initLabels()
        self.__initTextEdits()
        self.__initGenreWidget()
        self.__initDescriptionWidget()
        self.__initGenreFullWidget()
        self.__mainLayout.addWidget(self.__descriptionWidget)
        self.__mainLayout.addWidget(self.__genreFullWidget)
            
    def __initDescriptionWidget(self):
        """Initializes the item __descriptionWidget and adds its contained items."""
        layout = QtGui.QHBoxLayout()
        self.__descriptionWidget = QtGui.QWidget()
        layout.addWidget(self.__lblDescription)
        layout.addWidget(self.__txtDescription)
        self.__descriptionWidget.setLayout(layout)
        self.__descriptionWidget.setFixedHeight(60)
        
    def __initGenreFullWidget(self):
        """Initializes the item __genreFullWidget and adds its contained items."""
        hlayout = QtGui.QHBoxLayout()
        self.__genreFullWidget = QtGui.QWidget()
        hlayout.addWidget(self.__lblGenres)
        #vertical layout
        vlayout = QtGui.QVBoxLayout()
        vWidget = QtGui.QWidget()
        vlayout.addWidget(self.__genreArea)
        if self.__groupDetailType == GUIHelper.GroupDetailType.OWNER or self.__groupDetailType == GUIHelper.GroupDetailType.NEW_GROUP:
            self.__initButtons()
            self.__initButtonWidget()
            vlayout.addWidget(self.__buttonWidget)
        vWidget.setLayout(vlayout)
        hlayout.addWidget(vWidget)
        self.__genreFullWidget.setLayout(hlayout)
        
    def __initGenreWidget(self):
        """Initializes the item __checkGenre and adds its contained items."""
        self.__checkGenres = []
        #create gridLayout
        if self.__genres != None:
            gridLayout = QtGui.QGridLayout()
            #decide column
            column = 0
            row = 1
            for genre in self.__genres:
                genreName = genre.getName()
                isChecked = False
                if self.__group != None:
                    isChecked = self.__group.containsGenre(genreName)
                checkBox = GenreCheckBox(genreName, isChecked)
                checkBox.setEnabled(False)
                if self.__groupDetailType == GUIHelper.GroupDetailType.OWNER or self.__groupDetailType == GUIHelper.GroupDetailType.NEW_GROUP:
                    checkBox.setEnabled(True)
                if self.__group != None and self.__group.isActive() == False:
                    checkBox.setEnabled(False)
                gridLayout.addWidget(checkBox, row, column)
                self.__checkGenres.append(checkBox)
                column = (column + 1) % 4
                if column == 0:
                    row = row + 1
            #widgets for gridlayouts
            gridWidget = QtGui.QWidget()
            gridWidget.setLayout(gridLayout)
            self.__genreArea = QtGui.QScrollArea()
            self.__genreArea.setWidget(gridWidget)
                    
    def __initButtonWidget(self):
        """Initializes the item __buttonWidget and adds its contained items."""
        layout = QtGui.QHBoxLayout()
        self.__buttonWidget = QtGui.QWidget()
        layout.addWidget(self.__butSelectAll)
        layout.addWidget(self.__butDeselectAll)
        self.__buttonWidget.setLayout(layout)
        self.__buttonWidget.setFixedHeight(40)
                    
    def __initButtons(self):
        """Initializes the items __butSelectAll and __butDeselectAll."""
        self.__butSelectAll = QtGui.QPushButton(UIText.BUT_SELECT_ALL, self)
        self.__butSelectAll.setFocusPolicy(QtCore.Qt.NoFocus)
        self.connect(self.__butSelectAll, QtCore.SIGNAL('clicked()'), self.__selectAll)
        self.__butDeselectAll = QtGui.QPushButton(UIText.BUT_DESELECT_ALL, self) 
        self.__butDeselectAll.setFocusPolicy(QtCore.Qt.NoFocus)
        self.connect(self.__butDeselectAll, QtCore.SIGNAL('clicked()'), self.__deselectAll)
        if self.__group != None and self.__group.isActive() == False:
            self.__butSelectAll.setEnabled(False)
            self.__butDeselectAll.setEnabled(False)
        
    def __initLabels(self):
        """Initializes the items __lblDescription and __lblGenres."""
        self.__lblDescription = QtGui.QLabel(UIText.LABEL_DESCRIPTION + ":")
        self.__lblDescription.setFont(MyStyle.getFont13Bold())
        self.__lblGenres = QtGui.QLabel(UIText.LABEL_GENRES + ":")
        self.__lblGenres.setFont(MyStyle.getFont13Bold())
        if self.__group != None and self.__group.isActive() == False:
            self.__lblDescription.setEnabled(False)
            self.__lblGenres.setEnabled(False)
        
    def __initTextEdits(self):
        """Initializes the item __txtDescription."""
        self.__txtDescription = QtGui.QTextEdit()
        if self.__group != None and self.__group.getDescription() != None:
            self.__txtDescription.setText(self.__group.getDescription())
        if self.__group != None and self.__group.isActive() == False:
            self.__txtDescription.setDisabled(True)
        else:
            if self.__groupDetailType != GUIHelper.GroupDetailType.OWNER and self.__groupDetailType != GUIHelper.GroupDetailType.NEW_GROUP:
                self.__txtDescription.setReadOnly(True)
            
    def getDescription(self):
        """Returns the text of the item __txtDescription."""
        return unicode(str(self.__txtDescription.toPlainText()))
    
    def getGenres(self):
        """Returns the text of all items of the list __checkGenres that are checked. A list of Genre items is returned."""
        genres = []
        for checkBox in self.__checkGenres:
            if checkBox.isChecked():
                genres.append(PTPModel.Genre(checkBox.getGenrename()))
        return genres
    
    def __selectAll(self):
        """This method is connected to the QPushButton __butSelectAll."""
        for box in self.__checkGenres:
            box.setChecked(True)
    
    def __deselectAll(self):
        """This method is connected to the QPushButton __butDeselectAll."""
        for box in self.__checkGenres:
            box.setChecked(False)
            
    
class GenreCheckBox(QtGui.QCheckBox):
    """This class represents a check box containing a genre, one for each predefined genre is generated.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __isChecked: A boolean value, True if the check button should be checked, otherwise False.
        __genrename: A String object.
    """
    def __init__(self, genrename, isChecked):
        """The init-method."""
        QtGui.QCheckBox.__init__(self)
        self.__genrename = genrename
        self.__isChecked = isChecked
        self.__build()
        
    def __build(self):
        """Initializes the QCheckBox."""
        self.setText(self.__genrename)
        self.setFont(MyStyle.getFont8())
        self.setChecked(self.__isChecked)
        
    def isGenrenameEquals(self, gName, oName):
        """Returns True if the given name is equal to the item __genrename."""
        if self.__genrename == gName:
            return True
        return False
    
    def getGenrename(self):
        """Returns the item __genrename."""
        return self.__genrename
    
        
class GroupDetailTabMembers(QtGui.QWidget):
    """This class represents the group details member tab of the group details widget, where information about the member of the group are displayed.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __mainLayout: A QVBoxLayout object containing the items __contentWidget and eventually __buttonWidget.
        __group: The group the widget displays.
        __groupDetailType: Indicates how the group details should be displayed, one of OWNER, MEMBER, NOT_MEMBER or NEW_GROUP.
        __settings: A Settings object.
        __contentWidget: A QWidget object containing the items __memberListWidget and __keyDescriptionWidget.
        __memberListWidget: A MemberListWidget object connected to the method __memberListItemClicked() or a QLabel object.
        __keyDescriptionWidget: A QWidget object containing the items __lblIconGreen, __lblIconRed, __lblIconYellow, __lblIconBlack, __lblDescGreen, __lblDescRed, __lblDescYellow and __lblDescBlack.
        __lblIconGreen: A QLabel object.
        __lblIconRed: A QLabel object.
        __lblIconYellow: A QLabel object.
        __lblIconBlack: A QLabel object.
        __lblDescGreen: A QLabel object.
        __lblDescRed: A QLabel object.
        __lblDescYellow: A QLabel object.
        __lblDescBlack: A QLabel object.
        __buttonWidget: A QWidget object containing the items __butChat and eventually __butExcludeOrActivate.
        __butExcludeOrActivate: A QPushButton object connected to the method __excludeOrActivate().
        __butChat: A QPushButton object connected to the method chat().
    """
    def __init__(self, group, groupDetailType, settings, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__group = group
        self.__groupDetailType = groupDetailType
        self.__settings = settings
        self.__build()
        
    def __build(self):
        """Initializes the item __mainLayout and adds its contained items."""
        self.__cleanLayout()
        self.__initLabels()
        self.__initKeyDescription()
        self.__initMemberList()
        self.__initContentWidget()
        self.__mainLayout.addWidget(self.__contentWidget)
        #if there is at least one member
        if self.__group != None and self.__group.isActive() and self.__group.getMembers() != None and len(self.__group.getMembers()) > 0:
            # if i am the member/owner it has no sense to add the exclude or chat button
            if len(self.__group.getMembers()) > 1 or self.__groupDetailType == GUIHelper.GroupDetailType.NOT_MEMBER:
                self.__initButtonWidget()
                self.__mainLayout.addWidget(self.__buttonWidget)
            
    def __cleanLayout(self):
        """Removes all items from the widget."""
        import sip
        if self.layout() != None:
            oldLayout = self.layout()
            for i in reversed(range(oldLayout.count())):
                oldLayout.itemAt(i).widget().setParent(None)
            sip.delete(oldLayout)
        self.__mainLayout = QtGui.QVBoxLayout()
        self.setLayout(self.__mainLayout)

    def __initContentWidget(self):
        """Initializes the item __contentWidget and adds its contained items."""
        layout = QtGui.QHBoxLayout()
        self.__contentWidget = QtGui.QWidget()
        layout.addWidget(self.__memberListWidget)
        layout.addWidget(self.__keyDescriptionWidget)
        self.__contentWidget.setLayout(layout)
        
    def __initButtonWidget(self):
        """Initializes the item __buttonWidget and adds its contained items."""
        layout = QtGui.QHBoxLayout()
        self.__buttonWidget = QtGui.QWidget()
        self.__butExcludeOrActivate = None
        self.__initChatButton()
        layout.addWidget(self.__butChat)
        self.connect(self.__memberListWidget, QtCore.SIGNAL("itemClicked(QListWidgetItem*)"), self.__memberListItemClicked)
        if self.__groupDetailType == GUIHelper.GroupDetailType.OWNER:
            self.__initExcludeButton()
            layout.addWidget(self.__butExcludeOrActivate)
        self.__buttonWidget.setLayout(layout)
            
    def __initKeyDescription(self):
        """Initializes the item __keyDescriptionWidget and adds its contained items."""
        self.__keyDescriptionWidget = QtGui.QWidget()
        gridLayout = QtGui.QGridLayout()
        gridLayout.addWidget(self.__lblIconGreen, 1, 0)
        gridLayout.addWidget(self.__lblIconRed, 2, 0)
        gridLayout.addWidget(self.__lblIconYellow, 3, 0)
        gridLayout.addWidget(self.__lblIconBlack, 4, 0)
        gridLayout.addWidget(self.__lblDescGreen, 1, 1)
        gridLayout.addWidget(self.__lblDescRed, 2, 1)
        gridLayout.addWidget(self.__lblDescYellow, 3, 1)
        gridLayout.addWidget(self.__lblDescBlack, 4, 1)
        self.__keyDescriptionWidget.setLayout(gridLayout)
            
    def __initMemberList(self):
        """Initializes the item __memberListWidget and adds its contained items."""
        if self.__group != None and self.__group.getMembers() != None and len(self.__group.getMembers()) > 0:
            self.__memberListWidget = MemberListWidget(self.__settings, self, self)
            for member in self.__group.getMembers():
                isOwner = False
                if member.getName() == self.__group.getOwnername():
                    isOwner = True
                self.__memberListWidget.addMember(member.getName(), member.getStatus(), isOwner)
        else:
            self.__memberListWidget = QtGui.QLabel(UIText.MSG_NO_MEMBERS)
            self.__memberListWidget.setFont(MyStyle.getFont13())
        if self.__group != None and self.__group.isActive() == False:
            self.__memberListWidget.setDisabled(True)
            
    def __initLabels(self):
        """Initializes the items __lblDescRed, __lblDescGreen, __lblDescYellow, __lblDescBlack, __lblIconRed, __lblIconGreen, __lblIconYello and __lblIconBlack."""
        #descriptions
        self.__lblDescRed = QtGui.QLabel(UIText.LABEL_KEY_RED)
        self.__lblDescRed.setFont(MyStyle.getFont8())
        self.__lblDescRed.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        self.__lblDescGreen = QtGui.QLabel(UIText.LABEL_KEY_GREEN)
        self.__lblDescGreen.setFont(MyStyle.getFont8())
        self.__lblDescGreen.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        self.__lblDescYellow = QtGui.QLabel(UIText.LABEL_KEY_YELLOW)
        self.__lblDescYellow.setFont(MyStyle.getFont8())
        self.__lblDescYellow.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        self.__lblDescBlack = QtGui.QLabel(UIText.LABEL_KEY_BLACK)
        self.__lblDescBlack.setFont(MyStyle.getFont8())
        self.__lblDescBlack.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        #icons
        self.__lblIconRed = QtGui.QLabel()
        pixmap = QtGui.QPixmap(Resources.IMAGE_CIRCLE_RED)
        pixmap = pixmap.scaledToHeight(25)
        self.__lblIconRed.setPixmap(pixmap)
        self.__lblIconRed.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        self.__lblIconGreen = QtGui.QLabel()
        pixmap = QtGui.QPixmap(Resources.IMAGE_CIRCLE_GREEN)
        pixmap = pixmap.scaledToHeight(25)
        self.__lblIconGreen.setPixmap(pixmap)
        self.__lblIconGreen.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        self.__lblIconYellow = QtGui.QLabel()
        pixmap = QtGui.QPixmap(Resources.IMAGE_CIRCLE_YELLOW)
        pixmap = pixmap.scaledToHeight(25)
        self.__lblIconYellow.setPixmap(pixmap)
        self.__lblIconYellow.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        self.__lblIconBlack = QtGui.QLabel()
        pixmap = QtGui.QPixmap(Resources.IMAGE_CIRCLE_BLACK)
        pixmap = pixmap.scaledToHeight(25)
        self.__lblIconBlack.setPixmap(pixmap)
        self.__lblIconBlack.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        if self.__group != None and self.__group.isActive() == False:
            self.__lblDescRed.setEnabled(False)
            self.__lblDescGreen.setEnabled(False)
            self.__lblDescYellow.setEnabled(False)
            self.__lblDescBlack.setEnabled(False)
            self.__lblIconRed.setEnabled(False)
            self.__lblIconGreen.setEnabled(False)
            self.__lblIconYellow.setEnabled(False)
            self.__lblIconBlack.setEnabled(False)
            
    def __initExcludeButton(self):
        """Initializes the item __butExcludeOrActivate."""
        self.__butExcludeOrActivate = QtGui.QPushButton(UIText.BUT_EXCLUDE, self)
        self.__butExcludeOrActivate.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butExcludeOrActivate.setDisabled(True)
        self.__butExcludeOrActivate.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_STATE_CHANGE))
        self.connect(self.__butExcludeOrActivate, QtCore.SIGNAL('clicked()'), self.__excludeOrActivate)
        
    def __initChatButton(self):
        """Initializes the item __butChat."""
        self.__butChat = QtGui.QPushButton(UIText.BUT_CHAT, self)
        self.__butChat.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butChat.setIcon(QtGui.QIcon(Resources.IMAGE_CHAT_ICON))
        self.__butChat.setDisabled(True)
        self.connect(self.__butChat, QtCore.SIGNAL('clicked()'), self.chat)
        
    def __excludeOrActivate(self):
        """This method is connected to the QPushButton __butExcludeOrActivate."""
        selected = self.__memberListWidget.currentRow()
        membername = self.__memberListWidget.getMember(selected)
        status = self.__group.getMember(membername).getStatus()
        if status == PTPModel.MemberStatus.BLOCKED:
            text = UIText.MSG_REALLY_ACTIVATE_MEMBER
            newStatus = PTPModel.MemberStatus.ACTIVE
        else:
            text = UIText.MSG_REALLY_EXCLUDE_MEMBER
            newStatus = PTPModel.MemberStatus.BLOCKED
        reply = QtGui.QMessageBox.question(self, UIText.MSG_ATTENTION, text, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            groupname = self.__group.getGroupname()
            ownername = self.__group.getOwnername()
#            if membername != ownername:
            network.NetworkController.changeMemberStatus(membername, groupname, ownername, newStatus)
            #to set the correct icon when repainting
            self.__group.setMemberStatus(membername, newStatus)
            self.__build()
#            else:
#                QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_EXCLUDE_NOT_POSSIBLE_FOR_OWNER, UIText.BUT_OK)
            
    def __memberListItemClicked(self):
        """This method is connected to the QPushButton __memberListWidget."""
        selected = self.__memberListWidget.currentRow()
        membername = self.__memberListWidget.getMember(selected)
        if self.__butExcludeOrActivate != None:
            status = self.__group.getMember(membername).getStatus()
            if membername == self.__group.getOwnername():
                self.__butExcludeOrActivate.setText(UIText.BUT_EXCLUDE)
                self.__butExcludeOrActivate.setEnabled(False)
            else:
                member = self.__group.getMember(membername)
                if member!=None:
                    excludePossible = member.getStatus()!=PTPModel.MemberStatus.DELETED
                    self.__butExcludeOrActivate.setEnabled(excludePossible)
                else:
                    self.__butExcludeOrActivate.setEnabled(True)
                if status == PTPModel.MemberStatus.BLOCKED:
                    self.__butExcludeOrActivate.setText(UIText.BUT_ACTIVATE)
                else:
                    self.__butExcludeOrActivate.setText(UIText.BUT_EXCLUDE)
        myName = self.__settings.getCurrentUserSettings().getUsername()
        chatPossible = myName != membername
        self.__butChat.setEnabled(chatPossible)
        
            
    def chat(self):
        """This method is connected to the QPushButton __butChat."""
        selected = self.__memberListWidget.currentRow()
        chatPartnerName = self.__memberListWidget.getMember(selected)
        if self.__settings.getCurrentUserSettings().getUsername() != chatPartnerName:
            gui.GUIController.openPersonalMessageWidget(chatPartnerName)
        else:
            QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_CHAT_NOT_POSSIBLE, UIText.BUT_OK)
            
        
class MemberListWidget(QtGui.QListWidget):
    """This class represents the member list widget of the group details member tab inside the group details widget. It displays all members of the group.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __member: A List of Member objects.
        __settings: A Settings object.
        __memberTab: A GroupDetailTabMembers object where this widget is added to. 
    """
    def __init__(self, settings, memberTab, parent=None):
        """The init-method."""
        QtGui.QListWidget.__init__(self, parent)
        self.__parent = parent
        self.__member = []
        self.__settings = settings
        self.__memberTab = memberTab
        self.connect(self, QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem*)"), self.__doubleClick)
        
    def addMember(self, membername, status, isOwner):
        """Creates a new QListWidgetItem out of the given parameters, appends it to the widget and adds the member to the list __member."""
        item = QtGui.QListWidgetItem()
        if status == PTPModel.MemberStatus.ACTIVE:
            item.setIcon(QtGui.QIcon(Resources.IMAGE_CIRCLE_GREEN))
        elif status == PTPModel.MemberStatus.BLOCKED:
            item.setIcon(QtGui.QIcon(Resources.IMAGE_CIRCLE_RED))
        elif status == PTPModel.MemberStatus.NOT_CONFIRMED:
            item.setIcon(QtGui.QIcon(Resources.IMAGE_CIRCLE_YELLOW))
        else:
            item.setIcon(QtGui.QIcon(Resources.IMAGE_CIRCLE_BLACK))
        if isOwner:
            item.setText(membername + " (" + UIText.LABEL_OWNER + ")")
        else:
            item.setText(membername)
        item.setTextAlignment(QtCore.Qt.AlignLeft)
        item.setFont(MyStyle.getFont13())
        self.__member.append(membername)
        self.addItem(item)
        
    def getMember(self, index):
        """Returns the item of the list __member at the given position."""
        return self.__member[index]
    
    def __doubleClick(self):
        """This method is called when double clicking on the widget. Opens a chat window with the clicked item as chat partner."""
        selectedItem = self.selectedIndexes()[0].row()
        membername = self.__member[selectedItem]
        myName = self.__settings.getCurrentUserSettings().getUsername()
        chatPossible = myName != membername
        if chatPossible:
            self.__memberTab.chat()
